package com.sniper.web.freemarker;

import com.sniper.utils.JavaShellUtil;
import com.sniper.utils.ValidateUtil;
import com.sniper.web.utils.FilesUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

/**
 * 用户前台图片大小的展示
 *
 * @author sniper
 */
public class ImageHelpUtil {

    private final static Logger LOGGER = LoggerFactory.getLogger(ImageHelpUtil.class);

    /**
     * 显示图片的制定大小的缩略图，如果不存在生成，
     *
     * @param path
     * @param width
     * @param height
     * @return
     */
    public static String show(String path, int width, int height) {

        String webUrl = FilesUtil.getImagePrefix();

        if (!ValidateUtil.isValid(path)) {
            return webUrl + path;
        }

        if (width == 0 || height == 0) {
            return webUrl + path;
        }

        String rootPath = FilesUtil.getRootDir();

        if (!path.contains(rootPath)) {
            path = rootPath + path;
        }

        // 图片要改成的高度,宽度
        // 组装新图片地址
        String thumbPath = getThumbPath(path, width, height);

        //检测缩略图是否存在
        File thumbFile = new File(thumbPath);
        if (thumbFile.exists() && thumbFile.isFile()) {
            return webUrl + thumbPath.replace(rootPath, "");
        }
        //检查源文件是否存在
        File sourceFile = new File(path);
        if (!sourceFile.isFile()) {
            return webUrl + path.replace(rootPath, "");
        }

        //检查目录是否存在
        String newDir = thumbFile.getParent();
        File parentDir = new File(newDir);
        if (!parentDir.isDirectory()) {
            parentDir.mkdirs();
        }
        return thumb(path, width, height, rootPath, thumbPath);


    }

    /**
     * @param path
     * @param width
     * @param height
     * @return
     */
    public static String thumb(String path, int width, int height) {

        if (!ValidateUtil.isValid(path)) {
            return path;
        }

        if (width == 0 || height == 0) {
            return path;
        }

        String rootPath = FilesUtil.getRootDir();

        // 处理路径
        if (!path.contains(rootPath)) {
            path = rootPath + path;
        }

        // 图片要改成的高度,宽度
        // 组装新图片地址

        String thumbPath = getThumbPath(path, width, height);

        File file = new File(thumbPath);
        if (file.exists() && file.isFile()) {
            return thumbPath.substring(rootPath.length());
        }

        File oldFile = new File(path);
        if (!oldFile.isFile()) {
            return path.substring(rootPath.length());
        }

        String newDir = file.getParent();
        File parentDir = new File(newDir);
        if (!parentDir.isDirectory()) {
            parentDir.mkdirs();
        }

        return thumb(path, width, height, rootPath, thumbPath);

    }

    /**
     * 生成缩略图的主要方法
     *
     * @param path
     * @param width
     * @param height
     * @param rootPath
     * @param thumbPath
     * @return
     */
    private static String thumb(String path, int width, int height, String rootPath, String thumbPath) {
        try {
            Thumbnails.of(path).size(width, height).toFile(thumbPath);
            return thumbPath.substring(rootPath.length());
        } catch (Exception e) {
            String widthAndHeight = "";
            if (width > 0) {
                widthAndHeight += String.valueOf(width);
            }
            if (height > 0) {
                widthAndHeight += "x" + String.valueOf(height);
            }
            try {
                JavaShellUtil.executeShell("convert " + path + "  -resize '" + widthAndHeight + "' " + thumbPath);
                return thumbPath.substring(rootPath.length());
            } catch (IOException e1) {
                e1.printStackTrace();
                LOGGER.error(e.getMessage());
                return path.substring(rootPath.length());
            }
        }
    }

    /**
     * 获取缩略图的位置
     *
     * @param path
     * @param width
     * @param height
     * @return
     */
    public static String getThumbPath(String path, int width, int height) {
        // 组装新图片地址
        String suffix = FilesUtil.getFileExt(path);
        String thumbPath = path.substring(0, path.lastIndexOf(".")) + "_" + width + "_" + height + "." + suffix;
        thumbPath = thumbPath.replace("image", "thumb");
        return thumbPath;
    }

}
